package com.ruoyi.student.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.eduadmin.api.EduadminService;
import com.ruoyi.eduadmin.api.domain.TTeachTrace;
import com.ruoyi.student.domain.TClass;
import com.ruoyi.student.domain.TStudentDetail;
import com.ruoyi.student.dto.BatchUpdateStateDto;
import com.ruoyi.student.dto.InsertTrackDto;
import com.ruoyi.student.dto.RepeaterDto;
import com.ruoyi.student.dto.StudentMessageDto;
import com.ruoyi.student.mapper.TClassMapper;
import com.ruoyi.student.mapper.TStudentDetailMapper;
import com.ruoyi.student.vo.StudentVo;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.student.mapper.TStudentMapper;
import com.ruoyi.student.domain.TStudent;
import com.ruoyi.student.service.ITStudentService;

/**
 * 学生信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-12-24
 */
@Service
public class TStudentServiceImpl implements ITStudentService 
{
    @Autowired
    private TClassMapper tClassMapper;

    @Autowired
    private TStudentMapper tStudentMapper;

    @Autowired
    private TStudentDetailMapper tStudentDetailMapper;

    @Autowired
    private EduadminService eduadminService;


    /**
     * 查询学生信息
     * 
     * @param id 学生信息主键
     * @return 学生信息
     */
    @Override
    public TStudent selectTStudentById(Long id)
    {
        return tStudentMapper.selectTStudentById(id);
    }

    /**
     * 查询学生信息列表
     * 
     * @param tStudent 学生信息
     * @return 学生信息
     */
    @Override
    public List<TStudent> selectTStudentList(TStudent tStudent)
    {
        return tStudentMapper.selectTStudentList(tStudent);
    }

    /**
     * 新增学生信息
     * 
     * @param studentMessageDto 学生信息
     * @return 结果
     */
    @Override
    public int insertTStudent(StudentMessageDto studentMessageDto) {
        TStudent student1 = initStudent(studentMessageDto);
        tStudentMapper.insertTStudent(student1);

        TStudentDetail tStudentDetail = initStudentDetail(studentMessageDto);
        int i = tStudentDetailMapper.insertTStudentDetail(tStudentDetail);
        System.out.println(i);
        return i;
    }
    private TStudentDetail initStudentDetail(StudentMessageDto studentMessageDto) {
        TStudentDetail tStudentDetail = new TStudentDetail();
        tStudentDetail.setName(studentMessageDto.getName());
        tStudentDetail.setPhone(studentMessageDto.getPhone());
        tStudentDetail.setAge(studentMessageDto.getAge());
        tStudentDetail.setSex(studentMessageDto.getSex());
        tStudentDetail.setEducationBackground(studentMessageDto.getEducationBackground());
        tStudentDetail.setMajor(studentMessageDto.getMajor());
        tStudentDetail.setSchool(studentMessageDto.getSchool());
        tStudentDetail.setParentPhone(studentMessageDto.getParentPhone());
        return tStudentDetail;
    }

    private TStudent initStudent(StudentMessageDto studentMessageDto) {
        TStudent tStudent = new TStudent();
        tStudent.setState(studentMessageDto.getState());
        tStudent.setName(studentMessageDto.getName());
        tStudent.setClassId(studentMessageDto.getClassId());
        tStudent.setCampus(studentMessageDto.getCampus());
        tStudent.setMockInterview(studentMessageDto.getMockInterview());
        tStudent.setFinalTest(studentMessageDto.getFinalTest());
        return tStudent;
    }

    /**
     * 修改学生信息
     * 
     * @param tStudent 学生信息
     * @return 结果
     */
    @Override
    public int updateTStudent(TStudent tStudent)
    {
        return tStudentMapper.updateTStudent(tStudent);
    }

    /**
     * 批量删除学生信息
     * 
     * @param ids 需要删除的学生信息主键
     * @return 结果
     */
    @Override
    public int deleteTStudentByIds(Long[] ids)
    {
        return tStudentMapper.deleteTStudentByIds(ids);
    }

    /**
     * 删除学生信息信息
     * 
     * @param id 学生信息主键
     * @return 结果
     */
    @Override
    public int deleteTStudentById(Long id)
    {
        return tStudentMapper.deleteTStudentById(id);
    }

    @Override
    public int batchEdit(BatchUpdateStateDto batchUpdateStateDto) {
        List<Long> ids = batchUpdateStateDto.getIds();
        String finalTest = batchUpdateStateDto.getFinalTest();
        String mockInterview = batchUpdateStateDto.getMockInterview();
        Long state = batchUpdateStateDto.getState();
        int i = 0;
        for (Long id : ids) {
            TStudent student = tStudentMapper.selectTStudentById(id);
            student.setMockInterview(mockInterview);
            student.setFinalTest(finalTest);
            student.setState(state);
            i = tStudentMapper.updateTStudent(student);
        }
        return i;
    }
    /** 转班留级*/
    @Override
    public int repeater(RepeaterDto repeaterDto) {
        Long[] ids = repeaterDto.getId();
        TStudent student = tStudentMapper.selectTStudentById(ids[0]);
            student.setState(3L);
            student.setClassId(repeaterDto.getClassName());
            student.setRepeaterNode(repeaterDto.getNode());
        return tStudentMapper.updateTStudent(student);
    }
    /** 查询班级里的学生*/
    @Override
    public List<StudentVo> findStudentNameByClassId() {
        TStudent student = new TStudent();
        TClass tClass = new TClass();
        StudentVo studentVo = null;
        List<StudentVo> studentVos = new ArrayList<>();
        List<TClass> tClasses = tClassMapper.selectTClassList(tClass);
        List<TStudent> tStudents = tStudentMapper.selectTStudentList(student);
        for (TClass aClass : tClasses) {
            studentVo = new StudentVo();
            List<TStudent> students = tStudents.stream().filter(a1 -> aClass.getId().equals(a1.getClassId())).collect(Collectors.toList());
            List<String> studentNames = students.stream().map(TStudent::getName).collect(Collectors.toList());
            studentVo.setName(aClass.getName());
            studentVo.setNames(studentNames);
            studentVos.add(studentVo);
        }
        return studentVos;
    }
    /** 添加跟踪*/
    @Override
    public AjaxResult insertTrack(InsertTrackDto insertTrackDto) {

        LoginUser loginUser = SecurityUtils.getLoginUser();
        TTeachTrace tTeachTrace = new TTeachTrace();
        Long[] id = insertTrackDto.getId();
        System.out.println(Arrays.toString(id));
        Long studentId = id[0];
        TStudent student = tStudentMapper.selectTStudentById(studentId);
        tTeachTrace.setStudentId(student.getId());
        tTeachTrace.setStudentName(student.getName());
        tTeachTrace.setProblemType(insertTrackDto.getProblemType());
        tTeachTrace.setHappenTime(insertTrackDto.getHappenTime());
        tTeachTrace.setRemark(insertTrackDto.getRemark());
        tTeachTrace.setDeductMarks(insertTrackDto.getSocre());
        tTeachTrace.setTraceTeacher(loginUser.getUsername());
        TClass tClass = tClassMapper.selectTClassById(student.getClassId());
        tTeachTrace.setClassName(tClass.getName());
        return eduadminService.add(tTeachTrace);
    }

    @Override
    public List<TStudent> findStudentName(Long classesId) {
        List<TStudent> students = tStudentMapper.findStudentName(classesId);
        if (students == null) {
            throw new ServiceException("班级学生不存在");
        }
        return students;

    }

    @Override
    public TStudent findStudentByName(String name) {
        return tStudentMapper.findStudentByName(name);
    }

}
